package com.ls.lserp.modules.custom.web;

import com.ls.lserp.common.persistence.AjaxDone;
import com.ls.lserp.common.persistence.BaseEntity;
import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.utils.StringUtils;
import com.ls.lserp.common.web.BaseController;
import com.ls.lserp.modules.custom.entity.Size;
import com.ls.lserp.modules.custom.service.SizeService;
import com.ls.lserp.modules.sys.entity.Dict;
import com.ls.lserp.modules.sys.utils.DictUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;


/**
  * @ClassName: SizeController
  * @Description: 尺寸尺码
  * 
  * @author 曹学海
  * @date 2016年11月17日 下午3:06:26
  *
  */

@Controller
@RequestMapping(value = "${adminPath}/custom/size")
public class SizeController extends BaseController {
	
	// 保存 编辑 删除 方法
	@Autowired
	private SizeService sizeService;
	
	/**
	  * @Title: get
	  * @Description: 获取尺寸尺码信息
	  * @param @param id
	  * @param @return
	  * @return Size 
	  * @throws
	  */
	
	@ModelAttribute
	public Size get(@RequestParam(required=false) String id) {
		
		// 判断id是否为空
		if (StringUtils.isNotBlank(id)){
			
			// 执行查询语句 查询基本信息
			return sizeService.get(Long.parseLong(id));
		}else{
			
			// 若为空，实例化
			return new Size();
		}
	}
	
    /**
      * @Title: list
      * @Description: 查询页面
      * @param @param Size
      * @param @param request
      * @param @param response
      * @param @param model
      * @param @return
      * @return String 
      * @throws
      */
    
    @RequiresPermissions("custom:size:view")
    @RequestMapping(value = {"list", ""})
    public String list(Size size, HttpServletRequest request, HttpServletResponse response, Model model) {
        
    	// 查询一览页面数据
    	Page<Size> page = sizeService.findList(new Page<Size>(request, response), size);
        
    	// 将page值赋值给mvc model层返回jsp页面
    	model.addAttribute("page", page);
    	
    	// 返回页面地址
        return "modules/custom/size/sizeList";
    }

	/**
	  * @Title: form
	  * @Description: 新建及编辑尺码
	  * @param @param size
	  * @param @param redirectAttributes
	  * @param @param model
	  * @param @return
	  * @return String 
	  * @throws
	  */
	
	@RequiresPermissions("custom:size:edit")
	@RequestMapping(value = "form")
	public String form(Size size,RedirectAttributes redirectAttributes, Model model,HttpSession session) {
		
		// 执行查询语句
    	List<Size> list = sizeService.findSizeListChange(size);
        
    	size.setSizeList(list);
		// 将实体类值赋给mvc model层返回jsp页面
		model.addAttribute("size", size);
		
		// 返回页面地址
		return "modules/custom/size/sizeEdit";
	}
	
	
	@RequiresPermissions("custom:size:edit")
	@RequestMapping(value = "sizeListChange")
	public String sizeListChange(Size size,HttpServletRequest request, Model model) {
		List<Size> list= sizeService.findSizeListChange(size);
		
		size.setSizeList(list);
		// 将查询结果赋值mvc model层返回jsp页面
		model.addAttribute("size", size);
		return "modules/custom/size/sizeEdit";
	}
    /**
      * @Title: styleSize
      * @Description: 尺码联动
      * @param @param code
      * @param @param model
      * @param @param response
      * @param @return
      * @return String 
      * @throws
      */
    
    @RequiresPermissions("custom:size:view")
    @RequestMapping(value = "styleSize")
    @ResponseBody
    public String styleSize(String code,Model model,HttpServletResponse response ) {
        
        List<Dict> styleSizeList=DictUtils.getDictList(code);
        
        return renderString(response, styleSizeList);
       
    }
	
	
		/**
		  * @Title: save
		  * @Description: 保存数据
		  * @param @param Category
		  * @param @param request
		  * @param @param model
		  * @param @param response
		  * @param @return
		  * @return String 
		  * @throws
		  */
		
		@RequiresPermissions("custom:size:edit")
		@RequestMapping(value = "save")
		@ResponseBody
		public String save(Size size, HttpServletRequest request, Model model, HttpServletResponse response , HttpSession session) {

			Size sizeCheck = sizeService.getCheckSize(size.getStyleCategoryCd(),size.getStyleSizeGroupCd(),size.getStyleSizeCd(),size.getStyleFitCd());
            // 判断是否重复数据
			if (sizeCheck!=null&&size.getId()==null) {
					return renderString(response,AjaxDone.error(getText("size_exist")));
			}
			
			// 判断id是否为null （判断新建或修改）
			if(size.getId()!=null){
				// 编辑数据
				// 判断数据是否删除
				Size sizeNew=get(size.getId().toString());
				// 判断数据是否删除
	            if(sizeNew==null || BaseEntity.DEL_FLAG_DELETE.equals(sizeNew.getDeleteFlag())){
	                
	            	// 返回提示信息
	            	return renderString(response,AjaxDone.error(getText("This_record_has_been_deleted_or_modified_the_operation_failed")));
	            }
	            
	            // 排他处理
			    if(size.getVersion()==get(size.getId().toString()).getVersion()){
			    	
			    	// 版本号加1
			    	size.setVersion(size.getVersion()+1);
			    }else{
			    	
			    	// 返回提示信息
			        return renderString(response,AjaxDone.error(getText("This_record_has_been_deleted_or_modified_the_operation_failed")));
			    }
			}else{
				
				// 新建版本号赋值1
				size.setVersion(1L);
			}
			
			try {
				
				// 若启用未选择赋值1
				if(size.getEnableFlag()==null){
					size.setEnableFlag("1");
				}
				
			    // 保存分类
				sizeService.saveSize(size);
				
				// 保存成功返回提示信息
				return renderString(response,AjaxDone.ok(getText("size_save_success")));
			 } catch (Exception e) {
	           // 记录日志
	           logger.error(e.getMessage(), e);
				// 提示错误信息
		        return renderString(response,AjaxDone.error(getText("System_error_please_contact_your_administrator")));
		     }
		}
	
	
    /**
      * @Title: show
      * @Description: 查看分类
      * @param @param category
      * @param @param model
      * @param @return
      * @return String 
      * @throws
      */
    
    @RequiresPermissions("custom:size:view")
    @RequestMapping(value = "show")
    public String show(Size size, Model model) {
    	
    	// 执行查询语句
    	List<Size> sizeList = sizeService.findSizeListChange(size);
        
        // 将查询结果赋值mvc model层返回jsp页面
        model.addAttribute("sizeList", sizeList);
        
        // 将查询结果赋值mvc model层返回jsp页面
        model.addAttribute("size", size);
        
        // 返回页面地址
        return "modules/custom/size/sizeShow";
    }
    
    /**
      * @Title: delete
      * @Description: 删除
      * @param @param Category
      * @param @param response
      * @param @return
      * @return String 
      * @throws
      */
    
    @RequiresPermissions("custom:size:edit")
	@RequestMapping(value = "delete")
	public String delete(Size size, HttpServletResponse response) {
		
		    try {
		    	List<Size> sizeList = sizeService.findSizeListChange(size);
		    	// 执行逻辑删除方法
		    	sizeService.deleteSize(sizeList);
            } catch (Exception e) {
                // 记录日志
                logger.error(e.getMessage(), e);
            	// 提示错误信息
                return renderString(response,AjaxDone.error(getText("System_error_please_contact_your_administrator")));
            }
		    
		    // 提示删除成功信息
			return renderString(response,AjaxDone.ok(getText("size_delete_success")));
    }
    
	@RequiresPermissions("custom:size:edit")
	@ResponseBody
	@RequestMapping(value = "checkSize")
	public String checkSize(String styleCategoryCd,String styleSizeGroupCd,String styleSizeCd,String styleFitCd,String id) {
		
        try {

    		Size size = sizeService.getCheckSize(styleCategoryCd,styleSizeGroupCd,styleSizeCd,styleFitCd);
            
    			if (size!=null) {
    			
                if(id.equals(size.getId().toString())){
                	
                	return "true";
                }else{
                	
                	return "false";
                }
            } else {
            	
            	return "true";
            }
            
        } catch (Exception e) {
            // 记录日志
            logger.error(e.getMessage(), e);
        }
        // 数据值重名
        return "false";
	}
    
    
}
